package com.agile.springboot.controller;
import com.agile.springboot.common.Result;
import com.agile.springboot.entity.Option;
import com.agile.springboot.entity.Question;
import com.agile.springboot.service.QuestionService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/question")
public class QuestionController {


    @Resource
    QuestionService questionService;

    /**
     * 新增题目
     * @param question 信息尽可能完整
     * @return 结果相关信息
     */
    @PostMapping("/addQuestion")
    public Result<?> addQuestion(@RequestBody Question question){
        if(questionService.addQuestion(question)==1){
            return Result.success(question, "添加题目成功");
        }
        return Result.error("-1","添加题目失败");
    }

    /**
     * 物理删除题目
     * @param question 题目实体
     * 所必须的参数为 id 题目id
     * @return 结果相关信息
     */
    @PostMapping("/deleteQuestion")
    public Result<?> deleteOption(@RequestBody Question question){
        if(questionService.pscDeleteQuestion(question.getId())==1){
            return Result.success(question, "删除题目成功");
        }
        return Result.error("-1","删除题目失败");
    }

    /**
     * 修改题目信息
     * @param question 题目实体
     * 所必须的参数为 id 题目id
     * @return 结果相关信息
     */
    @PostMapping("/updateQuestion")
    public Result<?> updateQuestion(@RequestBody Question question){
        if(questionService.updateQuestion(question)==1){
            return Result.success(question, "修改题目成功");
        }
        return Result.error("-1","修改题目失败");
    }

    /**
     * 获取题目——根据题目id
     * @param question 题目实体
     * 所必须的参数为 id 题目id
     * @return 题目 实体
     */
    @PostMapping("/getQuestionById")
    public Result<?> getQuestionById(@RequestBody Question question){
        Question questionReal = questionService.searchQuestionById(question.getId());
        if(questionReal!=null){
            return Result.success(questionReal, "获取题目成功");
        }
        return Result.error("-1","获取题目失败");
    }

    /**
     * 获取题目——根据问卷id
     * @param question 题目实体
     * 所必须的参数为 questionnaire_id 所属问卷id
     * @return Question 实体
     */
    @PostMapping("/getQuestionsByQuestionnaireId")
    public Result<?> getQuestionsByQuestionnaireId(@RequestBody Question question){
        List<Question> questions = questionService.GetQuestionsByQuestionnaireId(question.getQuestionnaire_id());
        if(!questions.isEmpty()){
            return Result.success(questions, "获取题目列表成功");
        }
        return Result.error("-1","获取题目列表失败");
    }
}
